VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "XMLDocument"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True

'
'Copyright (c) 2010 Andrew Wall
'
' http://www.opensource.org/licenses/mit-license.php
'
'Permission is hereby granted, free of charge, to any person obtaining a copy
'of this software and associated documentation files (the "Software"), to deal
'in the Software without restriction, including without limitation the rights
'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the Software is
'furnished to do so, subject to the following conditions:
'
'The above copyright notice and this permission notice shall be included in
'all copies or substantial portions of the Software.
'
'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
'THE SOFTWARE.
'

Option Explicit

Implements Document
Implements Text

Private mName As String
Private mAttributeKeys As New Collection
Private mAttributes As New Dictionary

Private mChildren As New Collection

Public Sub init(name As String)
mName = name
End Sub

Private Function Attributes() As String
Dim attr As Variant
For Each attr In mAttributeKeys
    Attributes = Attributes + " " + attr + "=""" + mAttributes.Item(attr) + """"
Next
End Function
Private Function Children() As String
Dim child As Text
For Each child In mChildren
    Children = Children + child.xml
Next
End Function
Private Function StringFrom(value As Variant) As String
Dim name As String
name = TypeName(value)
Select Case name
    Case "String"
        StringFrom = value
    Case "Integer", "Long", "Decimal", "Single", "Double", "Byte", "Currency", "Boolean", "Date"
        StringFrom = CStr(value)
    Case Else
        StringFrom = name
End Select
End Function
Private Function EncodedStringFrom(value As Variant) As String
EncodedStringFrom = gFactory.EncodeXMLChars(StringFrom(value))
End Function
Private Function Document_addChild(name As String) As Document
Dim child As Document
Set child = gFactory.NewDocument(name)
mChildren.Add child
Set Document_addChild = child
End Function

Private Sub Document_addText(TextValue As String)
mChildren.Add gFactory.NewText(TextValue)
End Sub

Private Property Get Document_attributeCount() As Integer
Document_attributeCount = mAttributes.Count
End Property

Private Sub Document_createAttribute(name As String, value As Variant)
If mAttributes.Exists(name) Then
    mAttributes.Item(name) = EncodedStringFrom(value)
Else
    mAttributeKeys.Add name
    mAttributes.Add name, EncodedStringFrom(value)
End If
End Sub

Private Property Get Document_name() As String
Document_name = mName
End Property

Private Property Get Document_xml() As String
If mChildren.Count > 0 Then
    Document_xml = "<" + mName + Attributes + ">" + Children + "</" + mName + ">"
Else
    Document_xml = "<" + mName + Attributes + "/>"
End If
End Property

Private Property Get Text_xml() As String
Text_xml = Document_xml
End Property
